import React, { useState } from 'react';
import { HomePage } from '../features/home/HomePage';
import { ChatPage } from '../features/chat/ChatPage';
import { RecommendationPage } from '../features/recommendation/RecommendationPage';
import { MedicalRecordPage } from '../features/medical-record/MedicalRecordPage';
import { HelpCenterPage } from '../features/others/helpView';
import { AnnouncementsPage } from '../features/others/newsNotice';
import { 
  SidebarProvider, 
  SidebarInset,
  Sidebar, 
  SidebarContent, 
  SidebarGroup, 
  SidebarMenu, 
  SidebarMenuItem,
  SidebarMenuButton,
  SidebarHeader,
  SidebarFooter
} from '../components/ui/sidebar';
import { 
  Home, 
  MessageCircle, 
  Stethoscope, 
  FileText, 
  Menu,
  User,
  LogOut
} from 'lucide-react';

type AppState = 'home' | 'chat' | 'recommendation' | 'record' | 'help' | 'notice';

export default function App() {
  const [currentState, setCurrentState] = useState<AppState>('home');
  const [symptoms, setSymptoms] = useState<string[]>([]);

  const handleStartVoiceChat = () => {
    setCurrentState('chat');
  };

  const handleStartTextChat = () => {
    setCurrentState('chat');
  };

  const handleLogin = () => {
    // 模拟登录功能
    console.log('登录功能');
  };

  const handleEndSession = () => {
    setCurrentState('home');
    setSymptoms([]);
  };

  const handleGoBack = () => {
    if (currentState === 'chat') {
      setCurrentState('home');
    } else if (currentState === 'recommendation') {
      setCurrentState('chat');
    } else if (currentState === 'record') {
      setCurrentState('recommendation');
    } else if (currentState === 'help') {
      setCurrentState('home');
    } else if (currentState === 'notice') {
      setCurrentState('home');
    }
  };

  const handleProceedToRecommendation = (symptomsData: string[]) => {
    setSymptoms(symptomsData);
    setCurrentState('recommendation');
  };

  const handleProceedToRecord = () => {
    setCurrentState('record');
  };

  const handleReturnHome = () => {
    setCurrentState('home');
    setSymptoms([]);
  };

  const renderCurrentPage = () => {
    switch (currentState) {
      case 'home':
        return (
          <HomePage
            onStartVoiceChat={handleStartVoiceChat}
            onStartTextChat={handleStartTextChat}
            onLogin={handleLogin}
            onNavigate={(page) => setCurrentState(page)}
          />
        );
      case 'chat':
        return (
          <ChatPage
            onEndSession={handleEndSession}
            onGoBack={handleGoBack}
            onProceedToRecommendation={handleProceedToRecommendation}
          />
        );
      case 'recommendation':
        return (
          <RecommendationPage
            symptoms={symptoms}
            onGoBack={handleGoBack}
            onProceedToRecord={handleProceedToRecord}
          />
        );
      case 'record':
        return (
          <MedicalRecordPage
            onGoBack={handleGoBack}
            onReturnHome={handleReturnHome}
          />
        );
        case 'help':
          return (
            <HelpCenterPage />
          );
        case 'notice':
          return (
            <AnnouncementsPage />
          )
      default:
        return null;
    }
  };

  return (
    <SidebarProvider>
      <div className="flex w-full h-screen overflow-hidden">
        <Sidebar>
          <SidebarHeader className="border-b">
            <div className="flex items-center gap-2 py-2">
              <div className="w-8 h-8 bg-gradient-to-br from-blue-500 to-green-500 rounded-lg flex items-center justify-center">
                <svg className="w-5 h-5 text-white" viewBox="0 0 24 24" fill="currentColor">
                  <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
                </svg>
              </div>
              <div className="flex flex-col">
                <span className="font-semibold text-sm">医联智导</span>
                <span className="text-xs text-gray-500">智能导诊系统</span>
              </div>
            </div>
          </SidebarHeader>
          <SidebarContent>
            <SidebarGroup>
              <SidebarMenu>
                <SidebarMenuItem>
                  <SidebarMenuButton 
                    onClick={() => setCurrentState('home')}
                    isActive={currentState === 'home'}
                    className="data-[active=true]:bg-blue-100"
                  >
                    <Home className="w-4 h-4" />
                    <span>首页</span>
                  </SidebarMenuButton>
                </SidebarMenuItem>
                <SidebarMenuItem>
                  <SidebarMenuButton 
                    onClick={() => setCurrentState('chat')}
                    isActive={currentState === 'chat'}
                    className="data-[active=true]:bg-blue-100"
                  >
                    <MessageCircle className="w-4 h-4" />
                    <span>智能问诊</span>
                  </SidebarMenuButton>
                </SidebarMenuItem>
                <SidebarMenuItem>
                  <SidebarMenuButton 
                    onClick={() => setCurrentState('recommendation')}
                    isActive={currentState === 'recommendation'}
                    className="data-[active=true]:bg-blue-100"
                  >
                    <Stethoscope className="w-4 h-4" />
                    <span>科室推荐</span>
                  </SidebarMenuButton>
                </SidebarMenuItem>
                <SidebarMenuItem>
                  <SidebarMenuButton 
                    onClick={() => setCurrentState('record')}
                    isActive={currentState === 'record'}
                    className="data-[active=true]:bg-blue-100"
                  >
                    <FileText className="w-4 h-4" />
                    <span>电子病历</span>
                  </SidebarMenuButton>
                </SidebarMenuItem>
              </SidebarMenu>
            </SidebarGroup>
          </SidebarContent>
          <SidebarFooter className="border-t">
            <SidebarMenu>
              <SidebarMenuItem>
                <SidebarMenuButton>
                  <User className="w-4 h-4" />
                  <span>用户中心</span>
                </SidebarMenuButton>
              </SidebarMenuItem>
              <SidebarMenuItem>
                <SidebarMenuButton>
                  <LogOut className="w-4 h-4" />
                  <span>退出登录</span>
                </SidebarMenuButton>
              </SidebarMenuItem>
            </SidebarMenu>
          </SidebarFooter>
        </Sidebar>
        <SidebarInset>
          {renderCurrentPage()}
        </SidebarInset>
      </div>
    </SidebarProvider>
  );
}